import jweixin from 'jweixin-module';
import platform from '@/wlkutils/platform';

class Wxsdk {
    constructor() {
        this.isWechat = () => {
            return navigator.userAgent.indexOf('MicroMessenger') > -1
        }
    }

    async reFormUrl(){
        let entryUrl = location.href;
        return entryUrl;
    }

    async initJssdk() {
        const uri = encodeURIComponent(await this.reFormUrl());
        const res = await uni.$u.http.post('wechat/jsSdk', { uri });
        jweixin.config({
            debug: res.data.debug,
            appId: res.data.appId,
            timestamp: res.data.timestamp,
            nonceStr: res.data.nonceStr,
            signature: res.data.signature,
            jsApiList: res.data.jsApiList,
            openTagList: res.data.openTagList,
        });
        return res;
    }

    async getLocation(callback) {
        if (!this.isWechat()) return;
        const res = await this.initJssdk();
        jweixin.ready(() => {
            jweixin.getLocation({
                type: 'gcj02',
                success: (res) => {
                    callback(res);
                },
                fail: () => {
                    console.log('%c微信H5sdk,getLocation失败：', 'color:green;background:yellow');
                },
            });
        });
    }
}

class Location extends Wxsdk {
    constructor() {
        super();
    }

    async openAddress(callback) {
        if (!this.isWechat()) return;
        const res = await this.initJssdk();
        jweixin.ready(() => {
            jweixin.openAddress({
                success: (res) => {
                    callback(res);
                },
                fail: () => {
                    console.log('%c微信H5sdk,openAddress失败：', 'color:green;background:yellow');
                },
                complete: () => {},
            });
        });
    }

    async getCurrentLocation(callback) {
        await this.getLocation(callback);
    }
}

class QRCode extends Wxsdk {
    constructor() {
        super();
    }

    async scanQRCode(callback) {
        if (!this.isWechat()) return;
        const res = await this.initJssdk();
        jweixin.ready(() => {
            jweixin.scanQRCode({
                needResult: 1,
                scanType: ['qrCode', 'barCode'],
                success: (res) => {
                    callback(res);
                },
                fail: () => {
                    console.log('%c微信H5sdk,scanQRCode失败：', 'color:green;background:yellow');
                },
            });
        });
    }
}

class Share extends Wxsdk {
    constructor() {
        super();
    }

    async share(data, callback) {
        if (!this.isWechat()) return;
        const res = await this.initJssdk();
        jweixin.ready(() => {
            const shareData = {
                title: data.title,
                desc: data.desc,
                link: data.path,
                imgUrl: data.imageUrl,
                success: (res) => {
                    callback(res);
                },
                complete: (res) => {
                    console.log(res)
                },
                cancel: (res) => {
                    console.log(res)
                },
            };
            jweixin.updateAppMessageShareData(shareData);
        });
    }
}

class Map extends Wxsdk {
    constructor() {
        super();
    }

    async openLocation(data, callback) {
        if (!this.isWechat()) return;
        const res = await this.initJssdk();
        jweixin.ready(() => {
            jweixin.openLocation({
                latitude: data.latitude,
                longitude: data.longitude,
            });
        });
    }

    async chooseImage(callback) {
        if (!this.isWechat()) return;
        const res = await this.initJssdk();
        jweixin.ready(() => {
            jweixin.chooseImage({
                count: 1,
                sizeType: ['compressed'],
                sourceType: ['album'],
                success: (rs) => {
                    callback(rs);
                },
            });
        });
    }

    async wxpay(data, callback) {
        if (!this.isWechat()) return;
        const res = await this.initJssdk();
        jweixin.ready(function() {
            jweixin.chooseWXPay({
                timestamp: data.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                nonceStr: data.nonceStr, // 支付签名随机串，不长于 32 位
                package: data.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
                signType: data.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                paySign: data.paySign, // 支付签名
                success: function(res) {
                    callback(res);
                },
                fail: function(res) {
                    console.log("%c微信H5sdk,chooseWXPay失败：",
                        "color:green;background:yellow");
                    callback(res);
                },
                cancel: function(res) {

                },
            });
        });
    }
}

export default {
    location: new Location(),
    qrcode: new QRCode(),
    share: new Share(),
    map: new Map(),
};
